package com.zhixun.mobile.modules.edu.web;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.education.entity.Binding;
import com.boxin.ims.modules.education.entity.Grade;
import com.boxin.ims.modules.education.entity.Student;
import com.boxin.ims.modules.education.entity.Usercentrelist;
import com.boxin.ims.modules.education.service.BindingService;
import com.boxin.ims.modules.education.service.GradeService;
import com.boxin.ims.modules.education.service.StudentService;
import com.boxin.ims.modules.education.service.UsercentrelistService;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.zhixun.mobile.common.utils.EncryptUtils;
import com.zhixun.mobile.common.utils.StringUtils;
import com.zhixun.mobile.modules.base.web.BaseController;
import com.zhixun.mobile.modules.edu.service.EduService;
import com.zhixun.mobile.modules.edu.utils.BindingUtils;
import com.zhixun.mobile.modules.goods.service.GoodsService;
import com.zhixun.mobile.sys.utils.AssertUtils;
import com.zhixun.mobile.sys.utils.MemberUtils;
import com.zhixun.mobile.sys.utils.SessionMemberUtils;

/**
 * @author Jakemanse
 * @date 2014-2-26
 * @function <p> </p>
 */
@Controller
@RequestMapping(value = "/edu")
public class EduMemberController extends BaseController {
	
	@Autowired
	private EduService eduLoginService;
	
	@Autowired
	private BindingService bindingService;
	
	@Autowired
	private WechatMemberService wechatMemberService;
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private GradeService gradeService;
	
	@Autowired
	private UsercentrelistService usercentrelistService;
	
	
	@RequestMapping(value = "bind")
	public String bind(Long uid,HttpServletRequest request,HttpServletResponse response,Model model) {
		
		String style = request.getParameter("style");
		style = this.isStyle(style);

		User user = new User();
		user.setId(uid);
		String message = "";

		String topage = request.getParameter("topage");
		String wechatId = request.getParameter("wechatId");
		String mobile = request.getParameter("mobile");
		String id = request.getParameter("uid");

		model.addAttribute("wechatId", wechatId);
		model.addAttribute("uid", id);
		request.setAttribute("style", style);

		model.addAttribute("topage", topage);

		if (StringUtils.isEmpty(wechatId) || StringUtils.isEmpty(id)) {
			message = "对不起,你的请求发生错误，请返回微信中回复【会员申请】重新注册，谢谢！";
			model.addAttribute("message", message);

			return "modules/edu/themes/" + style + "/reg";
		}

		WechatMember wechatMember = wechatMemberService.getByWechatMemberId(
				user, wechatId);

		if (wechatMember != null) { // 已经绑定过的会员

			WechatMember wechatMember2 = isLogin(request, "" + uid);

			if (wechatMember2 != null) {

				request.setAttribute("wechatMember", wechatMember);
				
				//获得用户中心列表
				   Usercentrelist usercentrelist=new Usercentrelist();
				   usercentrelist.setUser(user);
				   List<Usercentrelist> list=usercentrelistService.getUsercentrelistByUserId(usercentrelist);
				   
				   model.addAttribute("list", list);

				return "modules/edu/themes/" + style + "/memberCenter";
			} else {

				Map<String, Object> map = this.beforeLogin(id, wechatId);

				model.addAttribute("login_mobile", map.get("mobile"));
				model.addAttribute("pwd", map.get("pwd"));

				return "modules/edu/themes/" + style + "/login";
			}

		} else { // 导航到指定页

			model.addAttribute("reg", "reg");
		}
		model.addAttribute("message", message);

		return "modules/edu/themes/" + style + "/reg";
	}
	
	/**
	 * @author Jakemanse
	 * @time 2014-2-26 4:15:46
	 * @function <p>
	 * 
	 *           </p>
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws ParseException
	 * @throws IOException
	 * @throws NumberFormatException
	 */
	@RequestMapping(value = "login")// TODO 会员登录
	public String login(HttpServletRequest request,
			HttpServletResponse response, Model model,
			RedirectAttributes redirectAttributes)
			throws NumberFormatException, IOException, ParseException {

		String style = request.getParameter("style");

		style = this.isStyle(style);

		String uid = request.getParameter("uid");
		String pwd = request.getParameter("pwd");
		String wechatId = request.getParameter("wechatId");
		String mobile = request.getParameter("mobile");
		String topage = request.getParameter("topage");
		String page = "";

		if (StringUtils.isEmpty(uid) || StringUtils.isEmpty(wechatId)
				|| StringUtils.isEmpty(mobile)) {
			return "modules/edu/themes/" + style + "/login";
		}

		request.setAttribute("topage", topage);
		request.setAttribute("wechatId", wechatId);
		request.setAttribute("uid", uid);
		request.setAttribute("style", style);

		// 判断是否已经登录
		WechatMember member = null;
		member = SessionMemberUtils.getSessionMemberByUserId(request, uid);
		boolean login = false;
		if (AssertUtils.notNull(member)) {
			login = true;
		}
		if (member == null) {
			User user = new User();
			user.setId(Long.parseLong(uid));
			member = wechatMemberService.getByWechatMemberId(user, wechatId);
			WechatMember wechatMember = wechatMemberService.getByMobile(user,
					mobile);
			if (member != null && wechatMember != null) {
				if (member.getMobile().equals(wechatMember.getMobile())) {
					login = EncryptUtils.validatePassword(pwd,
							member.getPassword());
				}
				// /登录
				if (login) {
					SessionMemberUtils.addSessionValue(request,
							"member_" + uid, member);
				}
			} else {
				login = false;
			}
		}
		model.addAttribute("wechatMember", member);
		
		//下面是判断用户是否绑定学生
		Binding binding=new Binding();
		User user =new User();
		user.setId(Long.parseLong(uid));
		
		WechatMember wechatMember = wechatMemberService.getByMobile(user,
				mobile);
		binding.setWechatMember(wechatMember);
		List<Binding> bindingList=bindingService.getBindingByMember(binding);
		if(bindingList.size()==0){
			Student student=new Student();
			student.setFatherMobile(mobile);
			student.setMotherMobile(mobile);
			student.setUser(user);
			
			List<Student> studentList= studentService.getStudentByParentMobile(student);
			model.addAttribute("studentList", studentList);
			return "modules/edu/themes/" + style + "/binding";
		}
		
		
	     Binding temp=(Binding) request.getSession().getAttribute("binding_id");
	     if(temp==null){
	      if(bindingList.size()>0){
	       temp=bindingList.get(0);
	      }
	      SessionMemberUtils.addSessionValue(request, "binding_id", temp);
	     }
		model.addAttribute("binding", temp);
		
		if (login) {
			// TODO 跳转成功后可以通过topage调回原来的
			request.setAttribute("wechatMember", member);

			if (member.isRandomPassword()) {

				model.addAttribute("message",
						MemberUtils.FIRSTLOGINUPDATEPASSWORD);

				return this.toupdatePwd(request, response, model,
						redirectAttributes);
			} else {
				if (AssertUtils.notNullOrEmpty(topage)) {

					return "modules/edu/themes/" + style + "/"+topage;
				} else {
					
					//获得用户中心列表
					   Usercentrelist usercentrelist=new Usercentrelist();
					   usercentrelist.setUser(user);
					   List<Usercentrelist> list=usercentrelistService.getUsercentrelistByUserId(usercentrelist);
					   
					   model.addAttribute("list", list);
					
					return "modules/edu/themes/" + style + "/memberCenter";
				}
			}
		}

		Map<String, Object> map = this.beforeLogin(uid, wechatId);
		
		//获得用户中心列表
		Usercentrelist usercentrelist=new Usercentrelist();
		usercentrelist.setUser(user);
		List<Usercentrelist> list=usercentrelistService.getUsercentrelistByUserId(usercentrelist);
		
		model.addAttribute("list", list);
		
		model.addAttribute("login_mobile", map.get("mobile"));
		model.addAttribute("pwd", map.get("pwd"));
		model.addAttribute("errormessage", "您的手机号或密码不正确!");
		return "modules/edu/themes/" + style + "/login";
	}
	
	/**
	 * 跳转注册
	 *@author xiaoqian
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * 2014-4-17下午5:13:24
	 */
	@RequestMapping(value = "reg") // TODO 跳转注册
	public String register(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		String style = request.getParameter("style");

		style = this.isStyle(style);

		String uid = request.getParameter("uid");
		String topage = request.getParameter("topage");
		String wechatId = request.getParameter("wechatId");

		model.addAttribute("reg", "reg");
		model.addAttribute("uid", uid);
		model.addAttribute("topage", "reg");
		model.addAttribute("wechatId", wechatId);
		request.setAttribute("style", style);

		return "modules/edu/themes/" + style + "/reg";
	}
	
	
	@RequestMapping(value = "toBinding") // TODO 跳转注册
	public String toBinding(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		String style = request.getParameter("style");

		style = this.isStyle(style);

		String uid = request.getParameter("uid");
		String topage = request.getParameter("topage");
		String wechatId = request.getParameter("wechatId");

		model.addAttribute("reg", "reg");
		model.addAttribute("uid", uid);
		model.addAttribute("topage", "reg");
		model.addAttribute("wechatId", wechatId);
		request.setAttribute("style", style);
		
		WechatMember member = this.isLogin(request, uid);
		
		User user = new User();
		   user.setId(Long.parseLong(uid));

		if (AssertUtils.notNull(member)) {
			request.setAttribute("wechatMember", member);
			
			//获得用户中心列表
			Binding binding=new Binding();
			binding.setUser(user);
			binding.setWechatMember(member);
			
			List<Binding> bindingList=bindingService.findBindingByMember(binding);
				Student student=new Student();
				student.setFatherMobile(member.getMobile());
				student.setMotherMobile(member.getMobile());
				student.setUser(user);
				
				List<Student> studentList= studentService.getStudentByParentMobile(student);
				for (int i = 0; i < studentList.size(); i++) {
					for (int j = 0; j < bindingList.size(); j++) {
						if(studentList.get(i).getId()==bindingList.get(j).getStudent().getId()){
							studentList.remove(i);
						}
					}
				}
				model.addAttribute("studentList", studentList);
				return "modules/edu/themes/" + style + "/binding";
		} else {

			Map<String, Object> map = this.beforeLogin(uid, wechatId);

			model.addAttribute("login_mobile", map.get("mobile"));
			model.addAttribute("pwd", map.get("pwd"));
			return "modules/edu/themes/" + style + "/login";
		}
		
		
	}
	
	/**
	 * 返回主页
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	//TODO 返回主页
	@RequestMapping(value = "toIndex")
	public String toIndex(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		String style = request.getParameter("style");

		style = this.isStyle(style);

		String id = request.getParameter("uid"); // userId
		String wechatId = request.getParameter("wechatId"); // OpenId

		model.addAttribute("style", style);
		model.addAttribute("uid", id);
		model.addAttribute("wechatId", wechatId);

		String topage = request.getParameter("topage");

		model.addAttribute("topage", topage);

		WechatMember member = this.isLogin(request, id);
		
		User user = new User();
		   user.setId(Long.parseLong(id));

		if (AssertUtils.notNull(member)) {
			request.setAttribute("wechatMember", member);
			
			//获得用户中心列表
			   Usercentrelist usercentrelist=new Usercentrelist();
			   usercentrelist.setUser(user);
			   List<Usercentrelist> list=usercentrelistService.getUsercentrelistByUserId(usercentrelist);
			   
			   model.addAttribute("list", list);

			return "modules/edu/themes/" + style + "/memberCenter";
		} else {

			Map<String, Object> map = this.beforeLogin(id, wechatId);

			model.addAttribute("login_mobile", map.get("mobile"));
			model.addAttribute("pwd", map.get("pwd"));
			return "modules/edu/themes/" + style + "/login";
		}

	}
	
	
	/**
	 * 跳转登录
	 *@author xiaoqian
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * 2014-4-17下午5:13:47
	 */
	@RequestMapping(value = "tologin") // TODO 跳转登陆
	public String tologin(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String style = request.getParameter("style");

		style = this.isStyle(style);
		String topage = request.getParameter("topage");
		String id = request.getParameter("uid");
		String wechatId = request.getParameter("wechatId");
		model.addAttribute("uid", id);
		request.setAttribute("topage", topage);
		model.addAttribute("wechatId", wechatId);
		request.setAttribute("style", style);
		System.out.println("返回登录页面......");

		Map<String, Object> map = this.beforeLogin(id, wechatId);

		model.addAttribute("login_mobile", map.get("mobile"));
		model.addAttribute("pwd", map.get("pwd"));
		return "modules/edu/themes/" + style + "/login";
	}
	
	//TODO 退出登陆
		@RequestMapping(value = "logout")
		public String logout(Model model, HttpServletRequest request,
				HttpServletResponse response) {
			String style = request.getParameter("style");

			String topage = request.getParameter("topage");
			style = this.isStyle(style);

			request.setAttribute("style", style);

			String wechatId = request.getParameter("wechatId"); // OpenId
			String id = request.getParameter("uid");

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			model.addAttribute("topage", topage);
			SessionMemberUtils.removeSession(request, "member_" + id, id);

			Map<String, Object> map = this.beforeLogin(id, wechatId);

			model.addAttribute("login_mobile", map.get("mobile"));
			model.addAttribute("pwd", map.get("pwd"));
			return "modules/edu/themes/" + style + "/login";
		}
		
		/**
		 * 绑定学生
		 *@author xiaoqian
		 * @param model
		 * @param request
		 * @param response
		 * @return
		 * 2014-4-18下午2:46:26
		 */
		@RequestMapping(value = "bindingStudent")
		public String bindingStudent(Model model, HttpServletRequest request,
				HttpServletResponse response) {
			String style = request.getParameter("style");

			String topage = request.getParameter("topage");
			style = this.isStyle(style);

			request.setAttribute("style", style);

			String wechatId = request.getParameter("wechatId"); // OpenId
			String id = request.getParameter("uid");
			String student_id=request.getParameter("student_id");

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			model.addAttribute("topage", topage);
			User user = new User();
			user.setId(Long.parseLong(id));
			
			
			
			
			WechatMember  member=wechatMemberService.getByWechatMemberId(user, wechatId);
			Student student=studentService.getStudentById(Long.parseLong(student_id));
			
			Binding binding=new Binding();
			binding.setWechatMember(member);
			binding.setUser(user);
			
			binding.setStudent(student);
			binding.setName(student.getGrade().getName());
			
			binding.setState("0");
			
			bindingService.save(binding);
			
			//获得用户中心列表
			Usercentrelist usercentrelist=new Usercentrelist();
			usercentrelist.setUser(user);
			List<Usercentrelist> list=usercentrelistService.getUsercentrelistByUserId(usercentrelist);
			
			model.addAttribute("list", list);
			model.addAttribute("member", member);
			
			return "modules/edu/themes/" + style + "/memberCenter";
		}
		
		
		/**
		 * 人工选择
		 *@author xiaoqian
		 * @param model
		 * @param request
		 * @param response
		 * @return
		 * 2014-4-18下午3:51:47
		 */
		@RequestMapping(value = "humanChoose")
		public String humanChoose(Model model, HttpServletRequest request,
				HttpServletResponse response) {
			String style = request.getParameter("style");

			String topage = request.getParameter("topage");
			style = this.isStyle(style);

			request.setAttribute("style", style);

			String wechatId = request.getParameter("wechatId"); // OpenId
			String id = request.getParameter("uid");

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			model.addAttribute("topage", topage);
			
			User user = new User();
			user.setId(Long.parseLong(id));
			
			Grade grade=new Grade();
			grade.setUser(user);
			
			List<Grade>  gradeList=gradeService.getGradeListByUserId(grade);
			
			model.addAttribute("gradeList", gradeList);
			return "modules/edu/themes/" + style + "/humChoose";
		}
		
		@RequestMapping(value = "getStudentByGradeId")
		public void getStudentByGradeId(Model model, HttpServletRequest request,
				HttpServletResponse response) {
			
			String style = request.getParameter("style");

			String topage = request.getParameter("topage");
			style = this.isStyle(style);

			request.setAttribute("style", style);

			String wechatId = request.getParameter("wechatId"); // OpenId
			String id = request.getParameter("uid");

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			model.addAttribute("topage", topage);
			
			String grade_id=request.getParameter("grade_id");
			
			User user = new User();
			user.setId(Long.parseLong(id));
			
			Grade grade=new Grade();
			grade.setId(Long.parseLong(grade_id));
			
			
			Student student=new Student();
			student.setUser(user);
			student.setGrade(grade);
			
			List<Student> studentList=studentService.getStudentByClassId(student);
			
			List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
			
			for (int i = 0; i < studentList.size(); i++) {
				Map<String, Object> map =new HashMap<String,Object>();
				map.put("id", studentList.get(i).getId());
				map.put("name", studentList.get(i).getName());
				list.add(map);
			}
			
			writeJson(request, response, list);
		}
		
	
	/**
	 * 判断用户是否登陆
	 * 
	 * @return WechatMember null 没有登陆 ；
	 */

	public WechatMember isLogin(HttpServletRequest request, String uid) {

		WechatMember member = SessionMemberUtils.getSessionMemberByUserId(
				request, uid);

		if (AssertUtils.notNull(member)) {
			member = wechatMemberService.get(member.getId());
		}

		return member;
	}

	/**
	 * 判断商家样式
	 */
	public String isStyle(String style) {
		String string = "";

		if (AssertUtils.isNullOrEmpty(style)) {
			string = "default";
			// string = "ShiShangHong" ;

		} else {
			string = style;
		}

		return "YouErJiaoYu";
	}
	
	/**
	 * 跳到登陆页面前根据 uid 和 wechatID 查询 会员。 如果有， 返回 手机号
	 */
	public Map<String, Object> beforeLogin(String uid, String wechatId) {
		Map<String, Object> map = new HashMap<String, Object>();
		String mobile = "";
		String pwd = "";

		User user = new User();

		user.setId(Long.parseLong(uid));

		WechatMember wechatMember = wechatMemberService.getByWechatMemberId(
				user, wechatId);

		if (AssertUtils.notNull(wechatMember)) {

			mobile = wechatMember.getMobile();

			// 是否需要初始化密码
			if (StringUtils.isEmpty(wechatMember.getPassword())
					|| wechatMember.isRandomPassword()) {
				pwd = com.thinkgem.jeesite.common.utils.StringUtils
						.getRandPassword();
				String enpwd = EncryptUtils.MD5EncodeString(pwd);
				wechatMember.setPassword(enpwd);
				wechatMember.setIsRandomPassword(true);

				wechatMemberService.save(wechatMember);
			}

			if (AssertUtils.isNullOrEmpty(pwd)) {
				pwd = null;
			}

			map.put("mobile", mobile);
			map.put("pwd", pwd);
		}

		return map;
	}
	
	// 修改密码
		//TODO 修改密码

		@RequestMapping(value = "toupdatePwd")
		public String toupdatePwd(HttpServletRequest request,
				HttpServletResponse response, Model model,
				RedirectAttributes redirectAttributes) {

			String style = request.getParameter("style");

			style = this.isStyle(style);

			String id = request.getParameter("uid"); // userId
			String wechatId = request.getParameter("wechatId"); // OpenId

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			request.setAttribute("style", style);
			model.addAttribute("topage", "toupdatePwd");

			WechatMember member = this.isLogin(request, id);

			if (AssertUtils.notNull(member)) {

				Boolean isRandomPassword = false;

				isRandomPassword = member.isRandomPassword();

				request.setAttribute("isRandomPassword", isRandomPassword);

				return "modules/edu/themes/" + style + "/updatePassword";
			}

			else {

				Map<String, Object> map = this.beforeLogin(id, wechatId);

				model.addAttribute("login_mobile", map.get("mobile"));
				model.addAttribute("pwd", map.get("pwd"));
				return "modules/edu/themes/" + style + "/login";
			}

		}
		
		//TODO 修改密码处理
		@RequestMapping(value = "updatePwd")
		public String updatePwd(HttpServletRequest request,
				HttpServletResponse response, Model model,
				RedirectAttributes redirectAttributes) {

			String style = request.getParameter("style");

			style = this.isStyle(style);

			String id = request.getParameter("uid"); // userId
			String wechatId = request.getParameter("wechatId"); // OpenId

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			request.setAttribute("style", style);
			model.addAttribute("topage", "updatePwd");
			// oldpwd 原密码
			// password 新密码

			String oldpwd = request.getParameter("oldpwd");
			String password = request.getParameter("password");

			WechatMember member = this.isLogin(request, id);

			if (AssertUtils.notNull(member)) {

				Boolean isRandomPassword = false;

				isRandomPassword = member.isRandomPassword();

				if (isRandomPassword == true) {

					member.setPassword(EncryptUtils.MD5EncodeString(password));
					member.setIsRandomPassword(false);
					wechatMemberService.save(member);

					WechatMember wechatMember = this.isLogin(request, id);

					request.setAttribute("wechatMember", wechatMember);

					request.setAttribute("message", "您的密码修改成功");
					
					User user = new User();
					user.setId(Long.parseLong(id));
					
					//获得用户中心列表
					Usercentrelist usercentrelist=new Usercentrelist();
					usercentrelist.setUser(user);
					List<Usercentrelist> list=usercentrelistService.getUsercentrelistByUserId(usercentrelist);
					
					model.addAttribute("list", list);

					return "modules/edu/themes/" + style + "/memberCenter";

				}// 是随机密码
				else {
					Boolean pwdOk;
					pwdOk = EncryptUtils.validatePassword(oldpwd,
							member.getPassword());

					if (!pwdOk) {
						request.setAttribute("isRandomPassword", isRandomPassword);
						request.setAttribute("message", "原密码错误");

						return "modules/edu/themes/" + style + "/updatePassword";
					} else {
						member.setPassword(EncryptUtils.MD5EncodeString(password));
						member.setIsRandomPassword(false);
						wechatMemberService.save(member);

						WechatMember wechatMember = this.isLogin(request, id);

						request.setAttribute("wechatMember", wechatMember);

						request.setAttribute("message", "您的密码修改成功");
						
						//获得用户中心列表
							User user = new User();
						   user.setId(Long.parseLong(id));
						
						   Usercentrelist usercentrelist=new Usercentrelist();
						   usercentrelist.setUser(user);
						   List<Usercentrelist> list=usercentrelistService.getUsercentrelistByUserId(usercentrelist);
						   
						   model.addAttribute("list", list);
						
						return "modules/edu/themes/" + style + "/memberCenter";

					}
				}// 不是随机密码

			}

			else {

				Map<String, Object> map = this.beforeLogin(id, wechatId);

				model.addAttribute("login_mobile", map.get("mobile"));
				model.addAttribute("pwd", map.get("pwd"));

				return "modules/edu/themes/" + style + "/login";
			}

		}
		
		//TODO 我的充值
		@RequestMapping(value = "recharge")
		public String recharge(HttpServletRequest request,
				HttpServletResponse response, Model model,
				RedirectAttributes redirectAttributes) {

			String style = request.getParameter("style");

			style = this.isStyle(style);

			String id = request.getParameter("uid"); // userId
			String wechatId = request.getParameter("wechatId"); // OpenId

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			request.setAttribute("style", style);
			model.addAttribute("topage", "recharge");

			WechatMember member = this.isLogin(request, id);

			if (AssertUtils.notNull(member)) {

				request.setAttribute("wechatMember", member);
				return "modules/edu/themes/" + style + "/recharge";
			}else {

				Map<String, Object> map = this.beforeLogin(id, wechatId);

				model.addAttribute("login_mobile", map.get("mobile"));
				model.addAttribute("pwd", map.get("pwd"));

				return "modules/edu/themes/" + style + "/login";
			}

		}
		
		
		// ----修改个人资料
		//TODO 修改个人资料
		@RequestMapping(value = "doUpdatePersonInformation")
		public String doUpdatePersonInformation(HttpServletRequest request,
				HttpServletResponse response, Model model,
				RedirectAttributes redirectAttributes) {

			String style = request.getParameter("style");

			style = this.isStyle(style);

			String id = request.getParameter("uid"); // userId
			String wechatId = request.getParameter("wechatId"); // OpenId

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			request.setAttribute("style", style);
			model.addAttribute("topage", "doUpdatePersonInformation");

			WechatMember member = this.isLogin(request, id);

			if (AssertUtils.notNull(member)) {

				request.setAttribute("wechatMember", member);
				return "modules/edu/themes/" + style
						+ "/updatePersonInformation";
			}

			else {

				Map<String, Object> map = this.beforeLogin(id, wechatId);

				model.addAttribute("login_mobile", map.get("mobile"));
				model.addAttribute("pwd", map.get("pwd"));

				return "modules/edu/themes/" + style + "/login";
			}

		}
		
		// ----修改个人资料
		//TODO 修改个人资料处理
		@RequestMapping(value = "updatePersonInformation")
		public String updatePersonInformation(HttpServletRequest request,
				HttpServletResponse response, Model model,
				RedirectAttributes redirectAttributes) {

			String style = request.getParameter("style");

			style = this.isStyle(style);

			String id = request.getParameter("uid"); // userId
			String wechatId = request.getParameter("wechatId"); // OpenId

			String sex = request.getParameter("sex");
			String birthday = request.getParameter("birthday");
			String name = request.getParameter("name");
			String mid = request.getParameter("mid");

			System.out.println("sex" + sex);

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			request.setAttribute("style", style);
			model.addAttribute("topage", "updatePersonInformation");

			WechatMember member = this.isLogin(request, id);
			
			User user = new User();
			   user.setId(Long.parseLong(id));

			if (AssertUtils.notNull(member)) {

				Map<String, Object> map = new HashMap<String, Object>();

				// map.put("id", mid);
				// map.put("name", name);
				// map.put("sex", sex);
				// map.put("birthday", birthday);
				//
				// memberService.updateMemberInformation(map);

				member.setName(name);
				member.setSex(sex);
				member.setBirthday(birthday);

				wechatMemberService.save(member);

				model.addAttribute("message", "您的用户信息修改成功！！！");

				model.addAttribute("wechatMember", member);

				this.updateMemberSession(request, id, member);
				
				//获得用户中心列表
				   Usercentrelist usercentrelist=new Usercentrelist();
				   usercentrelist.setUser(user);
				   List<Usercentrelist> list=usercentrelistService.getUsercentrelistByUserId(usercentrelist);
				   
				   model.addAttribute("list", list);

				return "modules/edu/themes/" + style + "/memberCenter";
			}

			else {

				Map<String, Object> map = this.beforeLogin(id, wechatId);

				model.addAttribute("login_mobile", map.get("mobile"));
				model.addAttribute("pwd", map.get("pwd"));

				return "modules/edu/themes/" + style + "/login";
			}
		}
		
		// 更新session中用户信息
		public void updateMemberSession(HttpServletRequest request, String uid,
				WechatMember member) {
			SessionMemberUtils.addSessionValue(request, "member_" + uid, member);
		}
		
		// ----跳到设置支付密码页面
		//TODO 设置支付密码

		@RequestMapping(value = "dopayPassword")
		public String dopayPassword(HttpServletRequest request,
				HttpServletResponse response, Model model,
				RedirectAttributes redirectAttributes) {

			String style = request.getParameter("style");

			style = this.isStyle(style);

			String id = request.getParameter("uid"); // userId
			String wechatId = request.getParameter("wechatId"); // OpenId

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			request.setAttribute("style", style);
			model.addAttribute("topage", "dopayPassword");

			WechatMember member = this.isLogin(request, id);

			if (AssertUtils.notNull(member)) {

				return "modules/edu/themes/" + style + "/payPassword";
			}

			else {

				Map<String, Object> map = this.beforeLogin(id, wechatId);

				model.addAttribute("login_mobile", map.get("mobile"));
				model.addAttribute("pwd", map.get("pwd"));

				return "modules/edu/themes/" + style + "/login";
			}

		}
		
		// ----设置支付密码
		//TODO 设置支付密码处理
		@RequestMapping(value = "payPassword")
		public String payPassword(HttpServletRequest request,
				HttpServletResponse response, Model model,
				RedirectAttributes redirectAttributes) {

			String style = request.getParameter("style");

			style = this.isStyle(style);

			String id = request.getParameter("uid"); // userId
			String wechatId = request.getParameter("wechatId"); // OpenId

			String password = request.getParameter("password");

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			request.setAttribute("style", style);
			model.addAttribute("topage", "payPassword");

			WechatMember member = this.isLogin(request, id);

			if (AssertUtils.notNull(member)) {

				member.setPayPassword(EncryptUtils.MD5EncodeString(password));

				wechatMemberService.save(member);

				model.addAttribute("message", "您的支付密码设置成功");

				model.addAttribute("wechatMember", member);

				this.updateMemberSession(request, id, member);
				
				User user = new User();
				   user.setId(Long.parseLong(id));
				//获得用户中心列表
				   Usercentrelist usercentrelist=new Usercentrelist();
				   usercentrelist.setUser(user);
				   List<Usercentrelist> list=usercentrelistService.getUsercentrelistByUserId(usercentrelist);
				   
				   model.addAttribute("list", list);

				return "modules/edu/themes/" + style + "/memberCenter";
			}

			else {

				Map<String, Object> map = this.beforeLogin(id, wechatId);

				model.addAttribute("login_mobile", map.get("mobile"));
				model.addAttribute("pwd", map.get("pwd"));

				return "modules/edu/themes/" + style + "/login";
			}

		}
		
		// ----跳到修改我的密码页面
		//TODO 修改我的密码

		@RequestMapping(value = "doupdatepayPassword")
		public String doupdatepayPassword(HttpServletRequest request,
				HttpServletResponse response, Model model,
				RedirectAttributes redirectAttributes) {

			String style = request.getParameter("style");

			style = this.isStyle(style);

			String id = request.getParameter("uid"); // userId
			String wechatId = request.getParameter("wechatId"); // OpenId

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			request.setAttribute("style", style);
			model.addAttribute("topage", "doupdatepayPassword");

			WechatMember member = this.isLogin(request, id);

			if (AssertUtils.notNull(member)) {

				return "modules/edu/themes/" + style + "/updatePayPassword";
			}

			else {

				Map<String, Object> map = this.beforeLogin(id, wechatId);

				model.addAttribute("login_mobile", map.get("mobile"));
				model.addAttribute("pwd", map.get("pwd"));

				return "modules/edu/themes/" + style + "/login";
			}

		}
		
		// ----修改我的密码
		//TODO 修改我的密码处理

		@RequestMapping(value = "updatepayPassword")
		public String updatepayPassword(HttpServletRequest request,
				HttpServletResponse response, Model model,
				RedirectAttributes redirectAttributes) {

			String style = request.getParameter("style");

			style = this.isStyle(style);

			String id = request.getParameter("uid"); // userId
			String wechatId = request.getParameter("wechatId"); // OpenId

			String oldpassword = request.getParameter("oldpassword");
			String password = request.getParameter("password");

			model.addAttribute("wechatId", wechatId);
			model.addAttribute("uid", id);
			request.setAttribute("style", style);
			model.addAttribute("topage", "updatepayPassword");

			WechatMember member = this.isLogin(request, id);

			if (AssertUtils.notNull(member)) {

				Boolean pwdOk;
				pwdOk = EncryptUtils.validatePassword(oldpassword,
						member.getPayPassword());

				if (!pwdOk) {
					request.setAttribute("message", "原密码错误");

					return "modules/edu/themes/" + style + "/updatePayPassword";

				} else {
					member.setPayPassword(EncryptUtils.MD5EncodeString(password));

					wechatMemberService.save(member);

					model.addAttribute("message", "您的支付密码修改成功");

					model.addAttribute("wechatMember", member);

					this.updateMemberSession(request, id, member);
					
					User user = new User();
					   user.setId(Long.parseLong(id));
					
					//获得用户中心列表
					   Usercentrelist usercentrelist=new Usercentrelist();
					   usercentrelist.setUser(user);
					   List<Usercentrelist> list=usercentrelistService.getUsercentrelistByUserId(usercentrelist);
					   
					   model.addAttribute("list", list);

					return "modules/edu/themes/" + style + "/memberCenter";
				}

			}

			else {

				Map<String, Object> map = this.beforeLogin(id, wechatId);

				model.addAttribute("login_mobile", map.get("mobile"));
				model.addAttribute("pwd", map.get("pwd"));

				return "modules/edu/themes/" + style + "/login";
			}
		}
		
		
		@RequestMapping(value = "test")
		public String test(HttpServletRequest request,
				HttpServletResponse response, Model model,
				RedirectAttributes redirectAttributes) {

				return "redirect:/edu/bind?uid=2&wechatId=qq501849930";



		}
		
		
		
		
}
